import 'dart:async';

import 'package:flutter/material.dart';

class CountDownText extends StatefulWidget {
  final Function function;
  final TextStyle style;

  CountDownText({
    Key key,
    @required this.function,
    @required this.style,
  }) : super(key: key);

  @override
  _CountDownTextState createState() => _CountDownTextState();
}

class _CountDownTextState extends State<CountDownText> {
  Timer _timer;
  int _start = 60;

  @override
  void initState() {
    super.initState();

    _timer = Timer.periodic(
      Duration(seconds: 1),
      (timer) {
        setState(() {
          if (_start <= 1) {
            timer.cancel();
            widget.function();
          } else {
            _start--;
          }
        });
      },
    );
  }

  @override
  void dispose() {
    _timer.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Text(
      '${_start}s 后重试',
      style: widget.style,
    );
  }
}
